var JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;

const keys = require('./keys.js')
const secretOrPrivateKey = keys.secretOrPrivateKey

const User = require('../models/User.js')

var opts = {}
//这里拿到token
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = secretOrPrivateKey;
//每一次发送要验证token请求的时候都到这里来 拿到token 后根据这个token来拿到对应的user的信息
module.exports = passport =>{
  passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
      User.findOne({id: jwt_payload.id}, function(err, user) {
          if (err) {
              return done(err, false);
          }
          if (user) {
              return done(null, user);
          } else {
              return done(null, false);
          }
      });
  }));
}